package com.solomon.travel.common.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.aliyun.oss.OSSClient;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.model.ExceptionResponse;
import com.solomon.travel.common.po.User;
import com.solomon.travel.common.util.ConstantUtils;

public class BaseController {
	
	protected static Logger logger = Logger.getLogger(BaseController.class);
	private static String endpoint = "http://oss-cn-qingdao.aliyuncs.com";
	protected static String ossurl = "http://echowall-16492.oss-cn-qingdao.aliyuncs.com/"; 
	private static String accessKeyId = "AKDtPPxBTonwp5qv";
	private static String accessKeySecret = "UQs4RNyJuEUazk9c2QMbHqQtqrDTUi";
	protected static String bucketName = "echowall-16492";
	protected static String firstKey = "web";
	protected static OSSClient ossClient;
	
	static{
		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	}
	
	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	protected ApiResponse defaultResponse() {
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK);
	}
	
	protected User currentUser() {
		return (User) getRequest().getSession().getAttribute("user");
	}
	
	@ResponseBody
	@ExceptionHandler
	public ExceptionResponse handleException(HttpServletRequest request,Exception exception) {
		exception.printStackTrace();
		return new ExceptionResponse(exception);
	}
}
